import 'package:mybaseplus/base/tool/empty/FFEmpty.dart';

class FFGlobalSafe{

}

/// 从map安全取值
T? globalSafeValueFromMap<T>(dynamic map, dynamic key) {
  if (key == null) return null;
  if (FFEmpty.mapIsEmpty(map)) return null;

  Map tmpMap = map;
  if(tmpMap.containsKey(key) == false) return null;

  dynamic tmp = tmpMap[key];
  if (tmp is T) {
    return tmp;
  }
  return null;
}


/// 从List安全取值
T? globalSafeValueFromList<T>(dynamic list, int? index) {
  if (index == null) return null;
  if (FFEmpty.listIsEmpty(list)) return null;

  List tmpList = list;
  if (index < 0 || index >= tmpList.length) return null;

  dynamic tmp = tmpList[index];
  if (tmp is T) {
    return tmp;
  }
  return null;
}

/// 安全转换类型
T? globalSafeConvert<T>(dynamic value){
  if (value == null) {
    return null;
  }

  if (value is T) {
    T result = value as T;
    return result;
  }

  return null;
}